# -*- coding:utf-8 -*-
import os
from concurrent.futures import ThreadPoolExecutor
from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager
from info.models import Task
from info import create_app, db
from info.utils.utils import random_uuid

# 创建app对象
app = create_app("production")
app.ThreadPoolExecutor = ThreadPoolExecutor(max_workers=os.cpu_count() * 2)
# 创建manager对象，用于启动服务
manager = Manager(app)
# 将app与db关联
Migrate(app, db)
# 将迁移命令添加到manager中
manager.add_command("db", MigrateCommand)


@manager.option("-d", "-description", dest="description")
@manager.option("-mp", "-methodpath", dest="methodpath")
@manager.option("-mn", "-methodname", dest="methodname")
@manager.option("-c", "-category", dest="category")
@manager.option("-rnt", "-runNumTips", dest="runNumTips")
def create_super_user(description, methodpath, methodname, category, runNumTips):
    """将已完成的接口写入表中"""
    task = Task()
    task.id = random_uuid()
    # task.create_time = user.last_update_time = datetime.datetime.now()
    task.description = description
    task.method_path = methodpath
    task.method_name = methodname
    task.method_category = category
    task.status = "0"
    task.run_num_tips = runNumTips

    try:
        db.session.add(task)
    except Exception as e:
        print(f"添加接口信息失败\n{e}")
    else:
        db.session.commit()

    print(f"添加接口信息成功，description={description}")


if __name__ == '__main__':
    manager.run()
